package com.cxyzy.demo

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import java.lang.Math.abs

class MyViewPager(context: Context, attrs: AttributeSet?) : ViewPager(context, attrs) {
    private var lastX = 0f
    private var lastY = 0f
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            lastX = ev.x
            lastY = ev.y
        } else if (ev.action == MotionEvent.ACTION_MOVE) {
            var currentX = ev.x
            var currentY = ev.y
            var offsetX = abs(currentX - lastX)
            var offsetY = abs(currentY - lastY)
            if (offsetY < offsetX * 4) {
                parent.requestDisallowInterceptTouchEvent(true)
            }
        }
        return super.dispatchTouchEvent(ev)
    }
}